package com.cgt.xiaotao.product.controller;

import com.cgt.xiaotao.product.domain.dto.PageDTO;
import com.cgt.xiaotao.product.domain.po.Product;
import com.cgt.xiaotao.product.domain.query.ProductPageQuery;
import com.cgt.xiaotao.product.domain.vo.ProductListVO;
import com.cgt.common.utils.R;
import com.cgt.common.domain.query.PageQuery;
import com.cgt.xiaotao.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@Api(tags = "商品接口")
@Slf4j
@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
public class ProductController {
    private final IProductService productService;

    // 测试接口
    @GetMapping("test")
    public String test() {
        return "测试接口";
    }

    @ApiOperation("分页查询商品列表(标签条件查询)")
    @PostMapping("/page")
    public R<PageDTO<ProductListVO>> getPage(@RequestBody ProductPageQuery query) {
        try {
            PageDTO<ProductListVO> pageDTO = productService.getPage(query);
            return R.ok(pageDTO);
        } catch (Exception e) {
            log.error("分页查询失败: {}", e.getMessage(), e); // 确保类上有 @Slf4j 注解
            return R.fail("服务暂不可用");
        }
    }

    @ApiOperation("查询商品详情")
    @GetMapping("/{id}")
    public R<Product> getById(@PathVariable Long id) {
        try {
            Product product = productService.getById(id);
            return R.ok(product);
        } catch (Exception e) {
            log.error("查询商品详情失败: {}", e.getMessage(), e); // 确保类上有 @Slf4j 注解
            return R.fail("服务暂不可用");
        }
    }

    @GetMapping("/detail")
    public Product getProductById(@RequestParam Long id) {
            return productService.getById(id);
    }

}
